﻿#include "CreateCudeDialog.h"
#include "ui_CreateCudeDialog.h"

#include "GUIFrame/MainWindow.h"

#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelBox.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoShapeAgent.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractModel.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKAbstractMeshSizeInfoGenerator.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKGeometryMeshSize.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKMeshGenInterface.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKRegionMeshSizeGeom.h"

#include <QMessageBox>
#include <QTableWidgetItem>
#include <QSpacerItem>

namespace GUI {

    CreateCudeDialog::CreateCudeDialog(Core::FITKActionOperator* oper) : _oper(oper)
    {
        _mainWin = FITKAPP->getGlobalData()->getMainWindowT<MainWindow>();
        _ui = new Ui::CreateCudeDialog();
        _ui->setupUi(this);
        init();
    }

    CreateCudeDialog::CreateCudeDialog(Interface::FITKAbsGeoModelBox * obj, Core::FITKActionOperator* oper) :
        _obj(obj), _oper(oper)
    {
        _ui = new Ui::CreateCudeDialog();
        _ui->setupUi(this);
        init();
    }

    CreateCudeDialog::~CreateCudeDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void CreateCudeDialog::init()
    {
        Acoustics::FITKAcousticsGeometryData* geometryData = FITKAPP->getGlobalData()->getGeometryData<Acoustics::FITKAcousticsGeometryData>();
        if (geometryData == nullptr) return;

        QString name = geometryData->checkName(tr("Box-1"));
        _ui->lineEdit_Name->setText(name);
    }

    void CreateCudeDialog::on_pushButton_Cancel_clicked()
    {
        _oper->setArgs("objID", -1);
        this->reject();
    }

    void CreateCudeDialog::on_pushButton_Create_clicked()
    {
        if (checkValue() == false)return;

        Interface::FITKGeoCommandList* geometryData = FITKAPP->getGlobalData()->getGeometryData<Interface::FITKGeoCommandList>();
        if (geometryData == nullptr) return;

        QString name = _ui->lineEdit_Name->text();
        if (geometryData->getDataByName(name)) {
            QMessageBox::warning(this, "", tr("\"%1\" already exists and cannot be overwritten.").arg(name), QMessageBox::Ok);
            return;
        }

        Interface::FITKInterfaceGeometryFactory* geofactory = Interface::FITKInterfaceGeometryFactory::getInstance();
        if (geofactory == nullptr)return;

        _obj = dynamic_cast<Interface::FITKAbsGeoModelBox*>(geofactory->createCommand(Interface::FITKGeoEnum::FITKGeometryComType::FGTBox));
        if (_obj == nullptr)return;
        getDataFormWidget();
        _obj->setDataObjectName(name);
        _obj->update();
        geometryData->appendDataObj(_obj);

        if (_oper && _obj) {
            _oper->setArgs("objID", _obj->getDataObjectID());
        }
        this->accept();
    }

    bool CreateCudeDialog::checkValue()
    {
        auto outputMessage = [&](QString name, double value) {
            QMessageBox::critical(this, "", tr("%1 value : %2 error!").arg(name).arg(value), QMessageBox::Ok);
        };

        double value = _ui->lineEdit_Dimensions1->text().toDouble();
        if (value == 0) {
            outputMessage(tr("Dimensions X"), value);
            return false;
        }

        value = _ui->lineEdit_Dimensions2->text().toDouble();
        if (value == 0) {
            outputMessage(tr("Dimensions Y"), value);
            return false;
        }

        value = _ui->lineEdit_Dimensions3->text().toDouble();
        if (value == 0) {
            outputMessage(tr("Dimensions Z"), value);
            return false;
        }

        return true;
    }

    void CreateCudeDialog::setDataToWidget()
    {
        Interface::FITKAbsGeoModelBox* obj = dynamic_cast<Interface::FITKAbsGeoModelBox*>(_obj);
        if (obj == nullptr)return;

        double basicPoint[3] = { 0,0,0 };
        obj->getPoint1(basicPoint);
        _ui->lineEdit_BasicPoint1->setText(QString::number(basicPoint[0]));
        _ui->lineEdit_BasicPoint2->setText(QString::number(basicPoint[1]));
        _ui->lineEdit_BasicPoint3->setText(QString::number(basicPoint[2]));

        double dimensions[3] = { 0,0,0 };
        obj->getLength(dimensions);
        _ui->lineEdit_Dimensions1->setText(QString::number(dimensions[0]));
        _ui->lineEdit_Dimensions2->setText(QString::number(dimensions[1]));
        _ui->lineEdit_Dimensions3->setText(QString::number(dimensions[2]));

    }

    void CreateCudeDialog::getDataFormWidget()
    {
        Interface::FITKAbsGeoModelBox* obj = dynamic_cast<Interface::FITKAbsGeoModelBox*>(_obj);
        if (obj == nullptr)return;

        double basicPoint[3] = { 0,0,0 };
        basicPoint[0] = _ui->lineEdit_BasicPoint1->text().toDouble();
        basicPoint[1] = _ui->lineEdit_BasicPoint2->text().toDouble();
        basicPoint[2] = _ui->lineEdit_BasicPoint3->text().toDouble();
        obj->setPoint1(basicPoint);

        double dimensions[3] = { 0,0,0 };
        dimensions[0] = _ui->lineEdit_Dimensions1->text().toDouble();
        dimensions[1] = _ui->lineEdit_Dimensions2->text().toDouble();
        dimensions[2] = _ui->lineEdit_Dimensions3->text().toDouble();
        obj->setLength(dimensions);
    }
}
